package com.tutor.lzm.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.tutor.lzm.R;
import com.tutor.lzm.SplashActivity;
import com.tutor.lzm.app.Constant;
import com.tutor.lzm.fragment.BaseFragment;
import com.tutor.lzm.fragment.BecomeTutorFragment;
import com.tutor.lzm.fragment.HireTutorFragment;
import com.tutor.lzm.fragment.MainFragment;
import com.tutor.lzm.fragment.PersonFragment;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.rb_main)
    RadioButton rbMain;
    @BindView(R.id.rb_hire_tutor)
    RadioButton rbHireTutor;
    @BindView(R.id.rb_become_tutor)
    RadioButton rbBecomeTutor;
    @BindView(R.id.rb_person)
    RadioButton rbPerson;
    @BindView(R.id.main_bottom)
    RadioGroup mainBottom;
    @BindView(R.id.fragment_container)
    RelativeLayout fragmentContainer;
    @BindView(R.id.mainLayout)
    RelativeLayout mainLayout;


    private MainFragment mainFragment;
    private HireTutorFragment hireTutorFragment;
    private BecomeTutorFragment becomeTutorFragment;
    private PersonFragment personalFragment;
    private BaseFragment[] fragments;
    private String tag = "MainActivity";
    private int index;
    // 当前fragment的index
    private int currentTabIndex = 0;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        showFragment();
        mainBottom.setOnCheckedChangeListener(this);

    }

    private void showFragment() {
        if (fragments != null) {
            fragments = null;
        }
        mainFragment = new MainFragment();
        hireTutorFragment = new HireTutorFragment();
        becomeTutorFragment = new BecomeTutorFragment();
        personalFragment = new PersonFragment();
        fragments = new BaseFragment[]{mainFragment, hireTutorFragment, becomeTutorFragment, personalFragment};

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, mainFragment)
                .hide(mainFragment).add(R.id.fragment_container, hireTutorFragment)
                .hide(hireTutorFragment).add(R.id.fragment_container, becomeTutorFragment).hide(becomeTutorFragment)
                .add(R.id.fragment_container, personalFragment).hide(personalFragment)
                .show(mainFragment).commit();
//        rbMain.setSelected(true);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_main:
                index = 0;
                break;
            case R.id.rb_hire_tutor:
                index = 1;
                break;
            case R.id.rb_become_tutor:
                index = 2;
                break;
            case R.id.rb_person:
                index = 3;
                break;
        }
        if (currentTabIndex != index) {

            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.fragment_container, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        currentTabIndex = index;
    }

    /**
     * 在fragment的管理类中，我们要实现这部操作，而他的主要作用是，当D这个activity回传数据到
     * 这里碎片管理器下面的fragnment中时，往往会经过这个管理器中的onActivityResult的方法。
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*在这里，我们通过碎片管理器中的Tag，就是每个碎片的名称，来获取对应的fragment*/
        int size = notifyBackPressList.size();
        if (size > 0) {
            BaseFragment f = notifyBackPressList.get(size - 1);
        /*然后在碎片中调用重写的onActivityResult方法*/
            f.onActivityResult(requestCode, resultCode, data);
        }
    }



    /**
     * isExit:双击退出标志位
     *
     * @since 1.0.0
     */
    private Boolean isExit = false;

    /**
     * 双击退出程序 exitBy2Click(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选) void
     *
     * @throws
     * @since 1.0.0
     */
    private void exitBy2Click() {

        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {

                @Override
                public void run() {

                    isExit = false; // 取消退出
                }
            }, 2000);
        } else {
            Intent intent = new Intent(this, SplashActivity.class);
            intent.putExtra(Constant.isExit, true);
            startActivity(intent);
            finish();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // moveTaskToBack( false );
            exitBy2Click();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
